package com.kelvin.oocl.importor;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import com.kelvin.oocl.pojo.Book;
import com.kelvin.oocl.util.DateUtil;

public class XMLImportor {
	public static void main(String[] args) {
		new XMLImportor().generateBooks(10000, new File("book.xml"));
	}
	
	public void generateBooks(int counts,File file){
		if(null==file){
			return;
		}
		BufferedOutputStream out=null;
		String head = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
		try {
			if(!file.exists()){
				file.createNewFile();
			}
			out = new BufferedOutputStream(new FileOutputStream(file));
			out.write(head.getBytes());
			out.write("<books>".getBytes());
			Book book = null;
			for (int i = 1; i <= counts; i++) {
				if(i%2==0){
					book = new Book(i, "Ja"+i, new Date(), "Kel"+i, true, 45.22, "Bei"+i);
				}else {
					book = new Book(i, "Ja"+i, new Date(), "Kel"+i, false, 45.22, "Bei"+i);
				}
				out.write(("<book id=\""+book.getId().toString()+"\">").getBytes());
				out.write(("<bookName>"+book.getBookName()+"</bookName>").getBytes());
				out.write(("<publishTime>"+DateUtil.dateToStr(book.getPublishTime(), "yyyy-MM-dd HH:mm:ss")+"</publishTime>").getBytes());
				out.write(("<author>"+book.getAuthor()+"</author>").getBytes());
				out.write(("<newBook>"+book.isNewBook()+"</newBook>").getBytes());
				out.write(("<price>"+book.getPrice()+"</price>").getBytes());
				out.write(("<press>"+book.getPress()+"</press>").getBytes());
				out.write("</book>".getBytes());
			}
			out.write("</books>".getBytes());
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				if(null!=out){
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
}
